﻿using LiveCharts;
using LiveCharts.Wpf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SmartWeigh;
using SmartWeighMonitor.Model;
using SmartWeighMonitor.Utils;
using SmartWeighMonitor.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SmartWeighMonitor.Windows
{
    /// <summary>
    /// HisWeight_Window.xaml 的交互逻辑
    /// </summary>
    public partial class HisWeight_Window : Window
    {
        private String status = "1";
        private String id = "";
        SynchronizationContext _syncContext = null;
        HisWeightViewModel viewModel;

        public SeriesCollection seriesCollection { get; set; }
        public List<string> Labels { get; set; }

        public HisWeight_Window()
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;
            viewModel = new HisWeightViewModel();
            this.DataContext = viewModel;

            this.Loaded += Win_loaded;
        }

        private void Win_loaded(object sender, RoutedEventArgs e)
        {
            //实例化一条折线图
            LineSeries line1 = new LineSeries();
            //设置折线的标题
            line1.Title = "流量";
            //设置折线的形式
            line1.LineSmoothness = 1;
            //折线图的无点样式
            line1.PointGeometry = null;
            //添加横坐标
            Labels = new List<string>();

            //添加绘图的数据
            line1.Values = new ChartValues<double>();
            seriesCollection = new SeriesCollection();
            seriesCollection.Add(line1);

            chart01.DataContext = this;
            this.chart01.Series = seriesCollection;
            slx.Labels = Labels;

            LoadData();
        }

        private void radio_click_status(object sender, RoutedEventArgs e)
        {
            RadioButton button = (RadioButton)sender;
            status = (String)button.Tag;

            LoadData();
        }
        public void setId(String id)
        {
            this.id = id;
        }

        private void LoadData()
        {
            JObject paramObject = new JObject();
            paramObject.Add("status", status);
            paramObject.Add("guid", id);
            new Task(() =>
            {
                try
                {
                    String serverIp = App.GetConfig().HttpServerIp;
                    String serverPort = App.GetConfig().HttpServerPort;
                    String response = HttpPostHelper.doHttpPost("http://" + serverIp + ":" + serverPort + "/ytBill/queryBillHisWeight", paramObject.ToString());
                    JObject rspJson = JObject.Parse(response);
                    string code = rspJson["code"].ToString();
                    if (code.ToLower() == "200")
                    {
                        var jsonStr = rspJson["data"].ToString();
                        List<HisWeightModel> items = JsonConvert.DeserializeObject<List<HisWeightModel>>(jsonStr);

                        _syncContext.Post(SetDataList, items);
                    }
                }catch (Exception ex){}
            }).Start();
        }
        private void SetDataList(object obj)
        {
            viewModel.Items.Clear();

            if (null == obj) return;
            List<HisWeightModel> items = (List<HisWeightModel>)obj;
            Labels.Clear();
            seriesCollection[0].Values.Clear();
            foreach (HisWeightModel item in items)
            {
                viewModel.Items.Add(item);
                Labels.Add(item.ctime);
                seriesCollection[0].Values.Add(double.Parse(item.weight));
            }
        }
    }
}
